﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace btnet.pages
{
    public partial class git_log : System.Web.UI.Page
    {
        protected Security security;
        private string log_result;
        private string repo;
        protected string file_path;
        private string string_affected_path_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);

            security = new Security();
            security.check_security(HttpContext.Current, Security.ANY_USER_OK);


            // get info about commit

            string sql = @"
select gitcom_repository, gitcom_commit, gitap_path, gitcom_bug
from git_commits
inner join git_affected_paths on gitap_gitcom_id = gitcom_id
where gitap_id = $id
order by gitcom_commit desc, gitap_path";

            int gitap_id = Convert.ToInt32(Bug_Util.sanitize_integer(Request["id"]));

            string_affected_path_id = Convert.ToString(gitap_id);
            sql = sql.Replace("$id", string_affected_path_id);

            DataRow dr = DbUtil.get_datarow(sql);


            // check if user has permission for this bug
            int bugid = (int)dr["gitcom_bug"];

            int permission_level = Bug.get_bug_permission_level(bugid, security);
            if (permission_level == Security.PERMISSION_NONE)
            {
                Response.Write("You are not allowed to view this item");
                Response.End();
            }

            idlog.Value = string_affected_path_id;

            repo = (string)dr["gitcom_repository"];
            string commit = (string)dr["gitcom_commit"];
            file_path = (string)dr["gitap_path"];

            log_result = VersionControl.git_log(repo, commit, file_path);

        }


        ///////////////////////////////////////////////////////////////////////
        void write_line(int row, string commit, string author, string date, string path, string action, string msg)
        {
            Response.Write("<tr><td class=datad>" + commit);
            Response.Write("<td class=datad>" + author);
            Response.Write("<td class=datad>" + date);
            Response.Write("<td class=datad>" + path);
            Response.Write("<td class=datad>" + action);
            Response.Write("<td class=datad>" + msg.Replace(Environment.NewLine, "<br/>"));

            Response.Write("<td class=datad><a target=_blank href=git_view.aspx?id=" + string_affected_path_id
                + "&commit=" + commit
                + ">");

            Response.Write("view</a>");

            Response.Write("<td class=datad><a target=_blank href=git_blame.aspx?id=" + string_affected_path_id
                + "&commit=" + commit
                + ">");

            Response.Write("annotated</a>");

            Response.Write("<td class=datad><a id=" + commit
                + " href='javascript:sel_for_diff("
                + Convert.ToString(row)
                + ",\""
                + commit
                + "\")'>select for diff</a>");
        }


        ///////////////////////////////////////////////////////////////////////
        protected void fetch_and_write_history()
        {
            Regex regex = new Regex("\n");
            string[] lines = regex.Split(log_result);

            string commit = "";
            string author = "";
            string date = "";
            string path = "";
            string action = "";
            string msg = "";
            int row = 0;

            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].StartsWith("commit "))
                {
                    if (commit != "")
                    {
                        write_line(++row, commit, author, date, path, action, msg);
                        commit = "";
                        author = "";
                        date = "";
                        path = "";
                        action = "";
                        msg = "";
                    }

                    commit = lines[i].Substring(7);
                }
                else if (lines[i].StartsWith("Author: "))
                {
                    author = Server.HtmlEncode(lines[i].Substring(8));
                }
                else if (lines[i].StartsWith("Date: "))
                {
                    date = lines[i].Substring(8, 19);
                }
                else if (lines[i].StartsWith("    "))
                {
                    if (msg != "")
                    {
                        msg += Environment.NewLine;
                    }
                    msg += lines[i].Substring(4);
                }
                else if (lines[i].Length > 1 && lines[i][1] == '\t')
                {
                    action = lines[i].Substring(0, 1);
                    path = lines[i].Substring(2);
                }

            }

            if (commit != "")
            {
                write_line(++row, commit, author, date, path, action, msg);
            }
        }

    }
}
